// index.js
// const app = getApp()
const {
  envList
} = require('../../envList.js');
const db = wx.cloud.database()
const _ = db.command
Page({
  data: {
    iconList: [{
        img: '/images/img/品质保障.png',
        text: '品质保障'
      },
      {
        img: '/images/img/同城配送 (1).png',
        text: '全国同城'
      },
      {
        img: '/images/img/空中花园.png',
        text: '花奔协会'
      },
      {
        img: '/images/img/时间分析（小时）.png',
        text: '2小时达'
      }
    ],
    swiperList: [{
      id: 0,
      type: 'image',
      url: 'https://scpic.chinaz.net/files/pic/psd1/202002/zpsd2452.jpg'
    }, {
      id: 1,
      type: 'image',
      url: '../../images/swiper/f2.jpg',
    }, {
      id: 2,
      type: 'image',
      url: '../../images/swiper/f3.jpg'
    }, {
      id: 3,
      type: 'image',
      url: '../../images/swiper/f4.jpg'
    }, {
      id: 4,
      type: 'image',
      url: '../../images/swiper/f5.jpg'
    }],
    goodsList: [],
    shoppingtext: "", //搜索框的值
    item: '',
    getCart: []
  },
  onLoad() {
    this.towerSwiper('swiperList');
    // 初始化towerSwiper
    let that = this;
    // 查询hg_goods
    const db = wx.cloud.database()
    db.collection('hg_goods').get({
      success: function (res) {
        console.log(res.data);
        that.setData({
          goodsList: res.data
        })
      }
    })

    //获取openId
    wx.cloud.callFunction({
      name: 'getOpenId',
      complete: res => {
        console.log(res);
        wx.setStorageSync('myOpenId', res.result.openid)
      }
    })
  },
  //根据鲜花姓名查询 搜索
  shoppinginput() {
    //跳转到搜索页面 
    console.log("跳转到搜索页面");
    wx.navigateTo({
      url: '../hyt/search/search',
    })
  },
  //点击图片到商品详情页
  onImage(e) {
    console.log("图片下标位置：", e.currentTarget.dataset.index);
    let index = e.currentTarget.dataset.index;
    console.log(this.data.goodsList[index]._id);
    wx.navigateTo({
      url: '../hyt/details/details?_id=' + this.data.goodsList[index]._id,
      success(res) {
        console.log(res);
      }
    })
  },
  //加入购物车
  async addShopping(e) {
    console.log("获取商品数据：", e);
    let item = e.currentTarget.dataset.item;
    let that = this;
    let storage_gid;
    var myOpenId = wx.getStorageSync("myOpenId");
    //获取购物车List
    let shopCart = wx.getStorageSync('shopCart');
    shopCart.forEach(item => {
      storage_gid = item.gId
    })
    console.log("storage_gid");
    console.log(storage_gid==item.gId)
    if (storage_gid==item.gId) {
      db.collection('hg_shopping_cart').where({
        gId: _.eq(storage_gid)
      }).update({
        data: {
          num: _.inc(1)
        }
      })
    } else {
      db.collection('hg_shopping_cart').add({
        data: {
          'gId': item.gId,
          'img1': item.img1,
          'goodsName': item.goodsName,
          'price': item.price,
          'num': 1,
          'openid': myOpenId
        }
      }).then(res => {
        console.log("res", res);
        // wx.reLaunch({
        //   url: '../shopping/shopping?_id='+item._id
        // })
      })
    }
  },


DotStyle(e) {
  this.setData({
    DotStyle: e.detail.value
  })
},
// cardSwiper
cardSwiper(e) {
  this.setData({
    cardCur: e.detail.current
  })
},
// towerSwiper
// 初始化towerSwiper
towerSwiper(name) {
  let list = this.data[name];
  for (let i = 0; i < list.length; i++) {
    list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
    list[i].mLeft = i - parseInt(list.length / 2)
  }
  this.setData({
    swiperList: list
  })
},
// towerSwiper触摸开始
towerStart(e) {
  this.setData({
    towerStart: e.touches[0].pageX
  })
},
// towerSwiper计算方向
towerMove(e) {
  this.setData({
    direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
  })
},
// towerSwiper计算滚动
towerEnd(e) {
  let direction = this.data.direction;
  let list = this.data.swiperList;
  if (direction == 'right') {
    let mLeft = list[0].mLeft;
    let zIndex = list[0].zIndex;
    for (let i = 1; i < list.length; i++) {
      list[i - 1].mLeft = list[i].mLeft
      list[i - 1].zIndex = list[i].zIndex
    }
    list[list.length - 1].mLeft = mLeft;
    list[list.length - 1].zIndex = zIndex;
    this.setData({
      swiperList: list
    })
  } else {
    let mLeft = list[list.length - 1].mLeft;
    let zIndex = list[list.length - 1].zIndex;
    for (let i = list.length - 1; i > 0; i--) {
      list[i].mLeft = list[i - 1].mLeft
      list[i].zIndex = list[i - 1].zIndex
    }
    list[0].mLeft = mLeft;
    list[0].zIndex = zIndex;
    this.setData({
      swiperList: list
    })
  }
}

})